package com.crm.system.service.impl.reminditem;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.DateUtil;
import com.crm.model.entity.reminditem.SysRemindItem;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.reminditem.RemindItemQO;
import com.crm.model.vo.reminditem.RelatedRemindVO;
import com.crm.model.vo.reminditem.RemindItemVO;
import com.crm.service.reminditem.SysRemindItemDbService;
import com.crm.system.service.reminditem.SysRemindItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 提醒事项
 * @Author lixq-f
 * @Date 2021/4/21
 * @Version V1.0
 **/
@Service
public class SysRemindItemServiceImpl implements SysRemindItemService {


    @Autowired
    private SysRemindItemDbService itemDbService;


    /**
     * @Description: 添加事项
     * @Param: [item]
     * @Return: java.lang.Boolean
     * @Author: lixq-f
     * @Date: 2021/4/21
     **/
    @Override
    public Boolean insert(RemindItemQO itemQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysRemindItem item = new SysRemindItem();
        BeanUtils.copyProperties(itemQO, item);
        item.setCreateTm(DateUtil.getSysDate());
        item.setCreatePeopleId(userAccount.getAccountId());
        item.setRemindItemId(CommonUtils.getGUID());
        item.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        return itemDbService.insert(item);
    }


    /**
     * @Description: 删除事项
     * @Param: [id, deleteStatus]
     * @Return: java.lang.Boolean
     * @Author: lixq-f
     * @Date: 2021/4/21
     **/
    @Override
    public Boolean deleteById(String id) {
        return itemDbService.deleteById(id, (short) StatusCodeEnum.DELETE_CODE.getState());
    }

    /**
     * @Description: 根据主键获取事项
     * @Param: [id]
     * @Return: com.crm.model.entity.reminditem.SysRemindItem
     * @Author: lixq-f
     * @Date: 2021/4/21
     **/
    @Override
    public SysRemindItem selectRemindItemById(String id) {
        SysRemindItem item = itemDbService.selectRemindItemById(id);
        return item;
    }


    /**
     * @Description: 所有用户所有事项
     * @Return: java.util.List<com.crm.model.entity.reminditem.SysRemindItem>
     * @Author: lixq-f
     * @Date: 2021/4/21
     **/
    @Override
    public List<SysRemindItem> selectListByCreatePeopleId() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<SysRemindItem> items = itemDbService.selectListByCreatePeopleId(userAccount.getAccountId());
        return items;
    }


    /**
     * @Description: 编辑事项
     * @Param: [item]
     * @Return: java.lang.Boolean
     * @Author: lixq-f
     * @Date: 2021/4/21
     **/
    @Override
    public Boolean update(RemindItemQO itemQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysRemindItem item = new SysRemindItem();
        BeanUtils.copyProperties(itemQO, item);
        item.setUpdatePeopleId(userAccount.getAccountId());
        item.setUpdateTm(DateUtil.getSysDate());
        return itemDbService.update(item);
    }


    /**
     * @Description: 获取提醒事项
     * @Param: []
     * @Return: com.crm.model.vo.reminditem.RemindItemVO
     * @Author: lixq-f
     * @Date: 2021/4/22
     **/
    @Override
    public RemindItemVO getRemindItem() {

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<SysRemindItem> items = itemDbService.selectListByCreatePeopleId(userAccount.getAccountId());

        RemindItemVO vo = new RemindItemVO();
        if (CollectionUtils.isEmpty(items)) {
            return vo;
        }
        int count = (int) items.stream()
                .filter(item -> Boolean.TRUE.equals(DateUtil.getSysDate().equals(item.getSetTm())))
                .count();

        vo.setTodayCount(count);
        vo.setItemList(items);
        return vo;
    }

    /***
     * @Description:获取相关提醒列表
     * @param selectDate 查询日期
     * @param pageIndex/pageSize 页号/每页显示个数
     * @Author: luojie
     * @Date: 2021/4/27 8:41
     */
    @Override
    public DhPageInfo<RelatedRemindVO> selectRelatedRemindList(String selectDate,Integer pageIndex,Integer pageSize,String sortName,String sortOrder) throws ParseException {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = simpleDateFormat.parse(selectDate);

        //排序
        StringBuilder orderStr = new StringBuilder();
        if(!StringUtils.isBlank(sortName)&&!StringUtils.isBlank(sortOrder)){
            switch(sortName){
                case "setTm":
                {
                    orderStr.append("setTm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<RelatedRemindVO> relatedRemindList = itemDbService.selectRelatedRemindList(userAccount.getAccountId(),date,orderStr.toString());
        PageInfo<RelatedRemindVO> pageInfo = new PageInfo<RelatedRemindVO>(relatedRemindList);
        return new DhPageInfo<RelatedRemindVO>(pageInfo, relatedRemindList);
    }

}
